
#include <vector_types.h>
#include <math_functions.h>

struct Particula {
    float3 velocidade;
    float massaEspecifica;
    float pressao;
    float3 forcaPressao;
    float3 forcaVisc;
    float3 forcaAceleracao;
};

__global__ void MassaEspecificaKernel(float3* posicoesParticulasT, int numeroParticulas, int resolucaoGridX, int resolucaoGridY, unsigned int* values, float deltaXGrid, float deltaYGrid, unsigned int* offSet, unsigned int* count, float h, Particula* particulasT, float massa, float constGas, float massaEspecificaInicial) {
    int thread = threadIdx.x;
    int block = blockIdx.x;
    int dim = blockDim.x;
    int indiceValue = dim * block + thread;
    int jAtual = 0;
    int iAtual = 0;
    int jMin = 0;
    int jMax = resolucaoGridX;
    int iMin = 0;
    int iMax = resolucaoGridY;
    int idCell;
    int kInicial;
    int kFinal;
    float distancia;

    if (indiceValue < numeroParticulas) {
        int indiceParticulaAtual = values[indiceValue];
        jAtual = (posicoesParticulasT[indiceParticulaAtual].x / deltaXGrid);
        iAtual = (posicoesParticulasT[indiceParticulaAtual].y / deltaYGrid);

        //VERIFICAO DAS EXTREMIDADES.
        if (jAtual <= 0) {
            jMin = 0;
        } else {
            jMin = jAtual - 1;
        }
        if (jAtual >= (resolucaoGridX - 1)) {
            jMax = jAtual;
        } else {
            jMax = jAtual + 1;
        }
        if (iAtual <= 0) {
            iMin = 0;
        } else {
            iMin = iAtual - 1;
        }
        if (iAtual >= (resolucaoGridY - 1)) {
            iMax = (resolucaoGridY - 1);
        } else {
            iMax = iAtual + 1;
        }

        particulasT[indiceParticulaAtual].massaEspecifica = 0;

        //CALCULO DA MASSA ESPECIFICA.
        for (int i = iMin; i <= iMax; i++) {
            for (int j = jMin; j <= jMax; j++) {
                float massaEsp = 0;
                idCell = (i * resolucaoGridX) + j;
                kInicial = offSet[idCell];
                kFinal = offSet[idCell] + count[idCell];
                for (int k = kInicial; k < kFinal; k++) {
                    int indiceParticulaVizinha = values[k];
                    if (indiceParticulaVizinha != indiceParticulaAtual) {
                        distancia = sqrtf((posicoesParticulasT[indiceParticulaAtual].x - posicoesParticulasT[indiceParticulaVizinha].x) * (posicoesParticulasT[indiceParticulaAtual].x - posicoesParticulasT[indiceParticulaVizinha].x) + (posicoesParticulasT[indiceParticulaAtual].y - posicoesParticulasT[indiceParticulaVizinha].y) * (posicoesParticulasT[indiceParticulaAtual].y - posicoesParticulasT[indiceParticulaVizinha].y));
                        if (distancia < h) {
                            //posicoesParticulasT[indiceParticulaAtual].x = (posicoesParticulasT2[indiceParticulaAtual].x + posicoesParticulasT2[indiceParticulaVizinha].x) * (distancia/h) /2;
                            //posicoesParticulasT[indiceParticulaAtual].y = (posicoesParticulasT2[indiceParticulaAtual].y + posicoesParticulasT2[indiceParticulaVizinha].y) * (distancia/h) /2;
                            //posicoesParticulasT[indiceParticulaAtual].z = posicoesParticulasT[indiceParticulaAtual].z;
                            massaEsp += massa * (315 / (64 * 3.14 * powf(h, 9)) * powf((h * h - distancia * distancia), 3));
                        }
                    }
                    if (massaEsp <= 0) {
                        massaEsp = massa;
                    }
                }
                particulasT[indiceParticulaAtual].massaEspecifica += massaEsp;
            }
        }

        //CALCALA A PRESSAO NA PARTICULA.
        particulasT[indiceParticulaAtual].pressao = constGas * (particulasT[indiceParticulaAtual].massaEspecifica - massaEspecificaInicial);
    }
}

extern "C" void launch_MassaEspecificaKernel(unsigned int blocksPerGrid, unsigned int threadsPerBlock, float3* posicoesParticulasT, int numeroParticulas, int resolucaoGridX, int resolucaoGridY, unsigned int* values, float deltaXGrid, float deltaYGrid, unsigned int* offSet, unsigned int* count, float h, Particula* particulasT, float massa, float constGas, float massaEspecificaInicial) {
    // execute the kernel
    MassaEspecificaKernel << <blocksPerGrid, threadsPerBlock >> >(posicoesParticulasT, numeroParticulas, resolucaoGridX, resolucaoGridY, values, deltaXGrid, deltaYGrid, offSet, count, h, particulasT, massa, constGas, massaEspecificaInicial);
}

